﻿using Logistics.Manager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Logistics
{

    public partial class Login : Form
    {

        LoginManager loginManager = new LoginManager();
        Manager.tc_User objUser = new Manager.tc_User();
        public Login()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;

            string oraclePath = Application.StartupPath + @"\oracle32";
            Environment.SetEnvironmentVariable("PATH", oraclePath, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("NLS_LANG", "SIMPLIFIED CHINESE_CHINA.ZHS16GBK", EnvironmentVariableTarget.Process);

            if (!loginManager.PinConnection())//如果无法连接到oracle则提示
            {
                MessageBox.Show("无法连接到oracle");
                System.Environment.Exit(0);
            }



        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_Load(object sender, EventArgs e)
        {
            BindCmbData();
            txtPassword.Text = "123456";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            var id = Guid.Parse(cmbUsers.SelectedValue.ToString());
            var user = objUser.GetModel(id);
            bool isLogin = user?.Password == txtPassword.Text.Trim();
            if (!isLogin)
            {
                MessageBox.Show("密码不匹配，无法登陆。");
                return;
            }
            MyForms.User = user;
            var objPermissions = new tc_Permission();
            MyForms.ButtonPermissions = objPermissions.GetList(user.Id, Model.EPermissionType.Buttons).Where(m => m.IsGranted).ToList();
            var mForm = new StartupForm();
            mForm.WindowState = FormWindowState.Maximized;
            mForm.Show();

            this.Hide();
        }


        #region 绑定基础数据
        private void BindCmbData()
        {
            var ulist = objUser.GetList();
            var user = new Model.tc_User();
            user.UserName = "请选择";
            user.Id = Guid.Empty;
            ulist.Insert(0, user);
            cmbUsers.DataSource = ulist;
            cmbUsers.DisplayMember = "UserName";
            cmbUsers.ValueMember = "Id";
            if (cmbUsers.Items.Count > 1)
                cmbUsers.SelectedIndex = 1;
            else
                cmbUsers.SelectedIndex = 0;
        }
        #endregion


        private void Login_KeyDown(object sender, KeyEventArgs e)
        {
            //单键 
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    btnLogin_Click(this, EventArgs.Empty);
                    break;
            }

            //// 组合键

            //if (e.KeyCode == Keys.F1 && e.Modifiers == Keys.Control)         //Ctrl+F1
            //{
            //    btnShouYi_Click(this, EventArgs.Empty);
            //}


        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            //单键 
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    btnLogin_Click(this, EventArgs.Empty);
                    break;
            }
        }

        private void cmbUsers_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void reflectionImage1_Click(object sender, EventArgs e)
        {

        }
    }
}
